package web.servlet;

import domain.Account;
import domain.Cart;
import service.LogService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

public class SignOutServlet extends HttpServlet {

    private static final String MAIN_FORM = "/WEB-INF/jsp/catalog/main.jsp";
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession();
        Account loginAccount = (Account) session.getAttribute("loginAccount");
        System.out.println("loginAccount signOut: "+loginAccount);

        String strBackUrl = "http://" + req.getServerName() + ":" + req.getServerPort()
                + req.getContextPath() + req.getServletPath() + "?" + req.getQueryString();
        LogService logService = new LogService();
        String logInfo = logService.logInfo(" ") + strBackUrl + " 退出账号";
        logService.insertLogInfo(loginAccount.getUsername(), logInfo);

        session.removeAttribute("loginAccount");
        Cart cart=null;
        session.setAttribute("cart",cart);
        req.getRequestDispatcher(MAIN_FORM).forward(req,resp);
    }
}
